﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ActionServices;
namespace OpenIDHostController
{
    public partial class frmConfig : Form
    {
         string config = Application.StartupPath + "\\DATA\\config.conf";
         string drivername = "PostgreSQL ODBC Driver(UNICODE)";
         string connectionstring;
         string dbname;
         string host;
         string port;
         string userid;
         string password;
        public frmConfig()
        {
            InitializeComponent();
        }
 
        private void frmConfig_Load(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void cmdtest_Click(object sender, EventArgs e)
        {
            try
            {
                dbname = txtdbname.Text;
                host = txtserver.Text;
                port = txtportdata.Text;
                userid = txtuser.Text;
                password = txtpassword.Text;
                if (ck64bit.Checked)
                    drivername = "PostgreSQL Unicode(x64)";
                this.connectionstring = "Driver={" + drivername + "};database=" + dbname + ";server=" + host + ";port=" + port + ";uid=" + userid + ";sslmode=disable;readonly=0;protocol=7.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;socket=4096;unknownsizes=0;maxvarcharsize=255;maxlongvarcharsize=8190;debug=0;commlog=0;optimizer=0;ksqo=1;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;cancelasfreestmt=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;disallowpremature=0;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=0;lowercaseidentifier=0;gssauthusegss=0;xaopt=1;password=" + password;
                if(RegisterService.RegisterServices.DBService.TestConnect(connectionstring))
                    MessageBox.Show("Connect to database server is success");
                else
                    MessageBox.Show("Can not connect to database server");
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        void SaveConfig()
        {
            try
            {
                dbname = Helper.Security.EncryptString(txtdbname.Text);
                host = Helper.Security.EncryptString(txtserver.Text);
                port = Helper.Security.EncryptString(txtportdata.Text);
                userid = Helper.Security.EncryptString(txtuser.Text);
                password = Helper.Security.EncryptString(txtpassword.Text);
                if (ck64bit.Checked)
                    drivername = "PostgreSQL Unicode(x64)";
                this.connectionstring = "Driver={" + drivername + "};database=" + dbname + ";server=" + host + ";port=" + port + ";uid=" + userid + ";sslmode=disable;readonly=0;protocol=7.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;socket=4096;unknownsizes=0;maxvarcharsize=255;maxlongvarcharsize=8190;debug=0;commlog=0;optimizer=0;ksqo=1;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;cancelasfreestmt=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;disallowpremature=0;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=0;lowercaseidentifier=0;gssauthusegss=0;xaopt=1;password=" + password + "\r\n";
                File.Delete(config);
                File.AppendAllText(config, "Host='" + txtserver.Text + "' #server address\r\n");
                File.AppendAllText(config, "Port='" + txtport.Text + "' #port to open server\r\n");
                File.AppendAllText(config, "connectionstring=" + this.connectionstring + " #Connection string to connect to server database");
                MessageBox.Show("the config file is saved");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        void LoadConfig()
        {
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader(config);
            try
            {
               
                while ((line = file.ReadLine()) != null)
                {
                    var data = line.Split('#');
                    if(data[0].Contains("Host"))
                    {
                        txtserver.Text = data[0].Split('=')[1].Replace("'","").Replace(" ","");
                    }
                    if (data[0].Contains("Port"))
                    {
                        txtport.Text = data[0].Split('=')[1].Replace("'", "").Replace(" ","");
                    }
                    if (data[0].Contains("connectionstring"))
                    {
                       var datastring = data[0].Split(';');
                       ck64bit.Checked = datastring[0].Contains("x64");
                       txtdbname.Text = Helper.Security.DecryptString(datastring[1].Replace("database=",""));
                       txtportdata.Text = Helper.Security.DecryptString(datastring[3].Replace("port=",""));
                       txtuser.Text = Helper.Security.DecryptString(datastring[4].Replace("uid=", ""));
                       txtpassword.Text = Helper.Security.DecryptString(datastring[datastring.Length - 1].Replace("password=", ""));
                    }
                }

                file.Close();
            }
            catch { file.Close(); }
        }

        private void cmdsave_Click(object sender, EventArgs e)
        {
            SaveConfig();
        }

        private void cmdclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
